package com.tiger.ebenv.sample3;

import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DateFormat;

import javax.naming.InitialContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class StockPriceReaderBean implements javax.ejb.SessionBean {
	private javax.ejb.SessionContext sessionContext;

	public void setSessionContext(javax.ejb.SessionContext sessionContext) {
		this.sessionContext = sessionContext;
	}

	public void ejbCreate() throws javax.ejb.CreateException {
	}

	public StockPrices getStockPrices() throws java.io.IOException, StockPriceReaderException {
		try {
			InitialContext ctx = new InitialContext();
			URL url = (URL) ctx.lookup("java:comp/env/url/StockPriceServer");
			HttpURLConnection con = (HttpURLConnection) url.openConnection();
			int responseCode = con.getResponseCode();
			if (responseCode != HttpURLConnection.HTTP_OK) {
				throw new StockPriceReaderException("HTTP Response Code: " + responseCode);
			}

			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(con.getInputStream());

			NodeList dateNodeList = document.getElementsByTagName("date");
			if (dateNodeList.getLength() != 1)
				throw new StockPriceReaderException("date要素は必ず一つでなければなりません");
			DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
			StockPrices stockPrices;
			try {
				stockPrices = new StockPrices(formatter.parse(dateNodeList.item(0).getFirstChild().getNodeValue()));
			} catch (java.text.ParseException e) {
				throw new StockPriceReaderException("date要素の内容を解析できません");
			}

			NodeList nodeList = document.getElementsByTagName("stockPrice");
			for (int i = 0; i < nodeList.getLength(); i++) {
				Node stockPriceNode = nodeList.item(i);
				NodeList childNodeList = stockPriceNode.getChildNodes();
				String company = null;
				double price = -1.0;
				for (int j = 0; j < childNodeList.getLength(); j++) {
					Node childNode = childNodeList.item(j);
					if (childNode.getNodeName().equals("company")) {
						company = childNode.getFirstChild().getNodeValue();
					} else if (childNode.getNodeName().equals("price")) {
						price = Double.parseDouble(childNode.getFirstChild().getNodeValue());
					}
				}
				if (company == null || price < 0.0)
					throw new StockPriceReaderException("company要素もしくはprice要素が存在しません");
				stockPrices.addStockPrice(company, price);
			}
			return stockPrices;
		} catch (javax.naming.NamingException e) {
			throw new javax.ejb.EJBException(e);
		} catch (javax.xml.parsers.ParserConfigurationException e) {
			throw new javax.ejb.EJBException(e);
		} catch (org.xml.sax.SAXException e) {
			throw new javax.ejb.EJBException(e);
		}
	}

	public void ejbActivate() {
	}

	public void ejbPassivate() {
	}

	public void ejbRemove() {
	}
}
